package com.huwang.greendao.gen;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;


import com.example.huwang.model.HistoryBean;

import org.greenrobot.greendao.query.Query;

import java.util.List;

public class DbHistoryController {
    /**
     * Helper
     */
    private DaoMaster.DevOpenHelper mHelper;    //获取Helper对象
    /**
     * 数据库
     */
    private SQLiteDatabase db;
    /**
     * DaoMaster
     */
    private DaoMaster mDaoMaster;
    /**
     * DaoSession
     */
    private DaoSession mDaoSession;
    /**
     * 上下文
     */
    private Context mContext;
    /**
     * dao
     */
    private HistoryBeanDao mPersonInfoDao;

    private static DbHistoryController mDbController;

    /**
     * 初始化
     * @param context
     */
    public DbHistoryController(Context context) {
        this.mContext = context;
        mHelper = new DaoMaster.DevOpenHelper(context,"person.db",null);
        mDaoMaster = new DaoMaster(getWritableDatabase());
        mDaoSession = mDaoMaster.newSession();
        mPersonInfoDao = mDaoSession.getHistoryBeanDao();
    }

    /**
     * 获取可读数据库
     * @return
     */
    private SQLiteDatabase getReadableDatabase(){
        if (mHelper == null){
            mHelper = new DaoMaster.DevOpenHelper(mContext,"person.db",null);
        }
        SQLiteDatabase db  = mHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     * @return
     */
    private SQLiteDatabase getWritableDatabase(){
        if (mHelper == null){
            mHelper = new DaoMaster.DevOpenHelper(mContext,"person.db",null);
        }
        SQLiteDatabase db = mHelper.getWritableDatabase();
        return db;
    }

    /**
     * 获取单例
     */
    public static DbHistoryController getInstance(Context context){
        if (mDbController == null){
            synchronized (DbHistoryController.class){
                if (mDbController==null){
                    mDbController = new DbHistoryController(context);
                }
            }
        }

        return mDbController;
    }

    /**
     * 会自动判定是插入还是替换数据
     * @param historyBean
     */
    public void insertOrReplace(HistoryBean historyBean){
        mPersonInfoDao.insertOrReplace(historyBean);
    }

    /**
     * 插入一条记录，表里面要没有与之相同的记录
     * @param historyBean
     * @return
     */
    public long insert(HistoryBean historyBean){
        return mPersonInfoDao.insert(historyBean);
    }

    /**
     * 更新数据
     * @param historyBean
     */
//    public void update(HistoryBean historyBean){
//        HistoryBean mOldPersonInfo = mPersonInfoDao.queryBuilder()
//                .where(LoginBeanDao.Properties.Id.eq(loginBean.getId())).build().unique();//拿到之前的记录
//        if (mOldPersonInfo != null){
//            mOldPersonInfo.setUserName("aaaaaaaa");
//            mPersonInfoDao.update(mOldPersonInfo);
//        }
//    }


    /**
     * 按条件查询数据
     * @param wherecluse
     * @return
     */
    public List<HistoryBean> searchByWhere(String wherecluse){
        Query<HistoryBean> query=mPersonInfoDao.queryBuilder()
                .where(HistoryBeanDao.Properties.Name.eq(wherecluse))     /*查询name属性一致的数据*/
                .orderAsc(LoginBeanDao.Properties.Id)
                .build();

        return query.list();
    }

    /**
     * 查询所有数据
     * @return
     */
    public List<HistoryBean> searchAll(){
        List<HistoryBean> personInfos = mPersonInfoDao.queryBuilder().list();
        return personInfos;
    }

    /**
     * 删除数据
     * @param wherecluse
     */
    public void delete(String wherecluse){
        mPersonInfoDao.queryBuilder().where(HistoryBeanDao.Properties.Name.eq(wherecluse))   /*根据name属性查找，然后执行删除*/
                .buildDelete().executeDeleteWithoutDetachingEntities();
    }

    public void deleteAll(){
        mPersonInfoDao.queryBuilder()  /*根据name属性查找，然后执行删除*/
                .buildDelete().executeDeleteWithoutDetachingEntities();
    }
}
